* 6: Generates summary statistics, runs balance tests
* Tables A.3, A.4, A.5
* Figures A.2, A.3, A.4
gl data ".../Replication Files/data"
gl dofiles ".../Replication Files/do files"




* Table A3 
{
use "$data/Samras_GPs.dta", clear
keep lgd_id ExcelSheetName RsInLacs
keep if lgd_id!=.

* Merge in GP-level IDs 
merge 1:1 lgd_id using "$data/vill_lgd_census11_01.dta", gen(merge) keepusing(gp_pop)
keep if merge==3

gen fem_samr = strpos(ExcelSheet,"Mahila")!=0
gen gen_samr = 1 - fem_samr

keep if inrange(gp_pop,4836,5209)

sort gp_pop 
order gp_pop gen_samr fem_samr RsInLacs
br gp_pop gen_samr fem_samr RsInLacs
}
*



* Figure A2: # Observations in LGD, GP Names
{
import excel "$data/Gujarat_LGD.xlsx", sheet("Sheet1") firstrow clear
distinct SNo
di r(N)

import excel "$data/GP Names LGD.xlsx", sheet("Sheet1") cellrange(B4:I13364) firstrow clear
qui describe
di r(N)
}
*



* Figure A3
cd "$dofiles"
foreach c in 4001 5001 6001 8001 {
use "$data/vill_lgd_census11_01.dta", clear
duplicates drop gp_id, force

keep if inrange(gp_pop,`c'-1000,`c'+999)
DCdensity gp_pop, breakpoint(`c') generate(Xj Yj r0 fhat se_fhat) 
local h=r(bandwidth)/2
di `h'
drop Xj Yj r0 fhat se_fhat
DCdensity gp_pop, breakpoint(`c') h(`h') generate(Xj Yj r0 fhat se_fhat) graphname(McCraryat`c'.png)
drop Xj Yj r0 fhat se_fhat
}
* 



* Figure A4
{

* Demographics
{
use "$data/baseline.dta", clear
collapse (sum) TotalGeographicalAreainHect TotalHouseholds TotalMalePopulationofVillage TotalFemalePopulationofVilla TotalScheduledCastesMalePop TotalScheduledCastesFemaleP TotalScheduledTribesMalePop TotalScheduledTribesFemaleP (mean) gp_pop, by(gp_id)

drop if gp_id==.

gen prop_female = TotalFemalePopulationofVilla/(TotalMalePopulationofVillage+TotalFemalePopulationofVilla)
gen prop_sc = (TotalScheduledCastesMalePop+TotalScheduledCastesFemaleP)/(TotalMalePopulationofVillage+TotalFemalePopulationofVilla)
gen prop_st = (TotalScheduledTribesMalePop+TotalScheduledTribesFemaleP)/(TotalMalePopulationofVillage+TotalFemalePopulationofVilla)


la var prop_female "Proportion Female"
la var prop_sc "Proportion Scheduled Caste"
la var prop_st "Proportion Scheduled Tribe"
la var TotalGeographicalAreainHect "Geographical Area (Hectares)"

set more off
foreach out in prop_female prop_sc prop_st TotalGeographicalAreainHect  {

rdplot `out' gp_pop if inrange(gp_pop,4500,5500), c(5001) nbins(3) p(2) ci(95) graph_options(xlabel(4500 5001 5500, labsize(huge)) ylabel(, labsize(huge)) graphregion(color(white) margin(2 5 2 5)) xsc(titlegap(2)) xtitle(GP Population, size(22pt)) title("") legend(off))

}
*

}
*


* Public Goods
{
use "$data/baseline.dta", clear
gen secondary_srsecondary 	= GovtSecondarySchoolNumbers + PrivateSecondarySchoolNumbe + GovtSeniorSecondarySchoolNu + BN 
gen healthcenters_clinics = CommunityHealthCentreNumbers + PrimaryHealthCentreNumbers + PrimaryHeallthSubCentreNumb + MobileHealthClinicNumbers + TBClinicNumbers
gen hospitals = HospitalAllopathicNumbers 
gen welfarecenters = MaternityAndChildWelfareCent + FamilyWelfareCentreNumbers


* Fix A(1)/NA(2) coding
{
foreach x in TapWaterTreatedStatusA1N TapWaterTreatedFunctioningAl TapWaterUntreatedStatusA1 TapWaterUntreatedFunctioning CoveredWellStatusA1NA2 CoveredWellFunctioningAllrou UncoveredWellStatusA1NA UncoveredWellFunctioningAll HandPumpStatusA1NA2 HandPumpFunctioningAllround TubeWellsBoreholeStatusA1 TubeWellsBoreholeFunctioning SpringStatusA1NA2 SpringFunctioningAllroundthe RiverCanalStatusA1NA2 RiverCanalFunctioningAllroun TankPondLakeStatusA1NA2 TankPondLakeFunctioningAllr ClosedDrainageStatusA1NA OpenDrainageStatusA1NA2 CommunityToiletComplexinclud CommunityToiletComplexexclud RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof {
replace `x' = . if `x'==0
replace `x' = `x'-2
}
* Now, x=-1 means you have the good

collapse (sum) secondary_srsecondary healthcenters hospitals welfarecenters (min)  TapWaterTreatedStatusA1N TapWaterTreatedFunctioningAl TapWaterUntreatedStatusA1 TapWaterUntreatedFunctioning CoveredWellStatusA1NA2 CoveredWellFunctioningAllrou UncoveredWellStatusA1NA UncoveredWellFunctioningAll HandPumpStatusA1NA2 HandPumpFunctioningAllround TubeWellsBoreholeStatusA1 TubeWellsBoreholeFunctioning SpringStatusA1NA2 SpringFunctioningAllroundthe RiverCanalStatusA1NA2 RiverCanalFunctioningAllroun TankPondLakeStatusA1NA2 TankPondLakeFunctioningAllr ClosedDrainageStatusA1NA OpenDrainageStatusA1NA2 CommunityToiletComplexinclud CommunityToiletComplexexclud RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof (mean) gp_pop, by(gp_id)

drop if gp_id==.


foreach x in secondary_srsecondary healthcenters hospitals welfarecenters {
replace `x'=`x'>0 if `x'!=.
}


foreach x in TapWaterTreatedStatusA1N TapWaterTreatedFunctioningAl TapWaterUntreatedStatusA1 TapWaterUntreatedFunctioning CoveredWellStatusA1NA2 CoveredWellFunctioningAllrou UncoveredWellStatusA1NA UncoveredWellFunctioningAll HandPumpStatusA1NA2 HandPumpFunctioningAllround TubeWellsBoreholeStatusA1 TubeWellsBoreholeFunctioning SpringStatusA1NA2 SpringFunctioningAllroundthe RiverCanalStatusA1NA2 RiverCanalFunctioningAllroun TankPondLakeStatusA1NA2 TankPondLakeFunctioningAllr ClosedDrainageStatusA1NA OpenDrainageStatusA1NA2 CommunityToiletComplexinclud CommunityToiletComplexexclud RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof {
replace `x' = `x'==-1
}
}


gen tapwater = max(TapWaterTreatedStatusA1N, TapWaterUntreatedStatusA1)
gen drainage = max(ClosedDrainageStatusA1NA, OpenDrainageStatusA1NA2)
gen CommunityToilet = max(CommunityToiletComplexinclud,CommunityToiletComplexexclud)

la var secondary_srsecondary "Secondary/Senior Secondary Schools"
la var healthcenters "Health Centers/Clinics"
la var welfarecenters "Maternity/Child/Family Welfare Centers"
la var tapwater "Tap Water"

la var CoveredWellStatusA1NA "Covered Wells"
la var drainage "Closed Drainage"
la var CommunityToilet "Community Toilet Complex"
la var Communitywastedisposalsystem "Community Waste Disposal System"

set more off
foreach out in secondary_srsecondary healthcenters welfarecenters tapwater CoveredWellStatusA1NA drainage CommunityToilet Communitywastedisposalsystem {

rdplot `out' gp_pop if inrange(gp_pop,4500,5500), c(5001) nbins(3) p(2) ci(95) graph_options(xlabel(4500 5001 5500, labsize(huge)) ylabel(, labsize(huge)) graphregion(color(white) margin(2 5 2 5)) xsc(titlegap(2)) xtitle(GP Population, size(22pt)) title("") legend(off))

}
*

}
*

}
*



* Table A4; Numbers for Figure A2
{

* Panel 1
{
use "$data/elecdata_clean.dta", clear
distinct gp
di r(N)

gen prof_farm_bus = prof_ag_farmer + prof_bus

* Candidate Characteristics
foreach out in educ_years age female prof_farm_bus prof_jobs prof_ag_lab {

qui sum `out'
di "`out' " round(r(mean),.001) "   " round(r(sd),.001)

di ""
}
*

* Election Characteristics: Candidates/Seat
keep if age_winner!=.
qui sum candidates
di round(r(mean),.001) "   " round(r(sd),.001)


* Election Characteristics: Seats + Single-Candidate Seats
collapse (mean) maxwards (sum) nocontest, by(gp)
foreach out in maxwards nocontest {

qui sum `out'
di "`out' " round(r(mean),.001) "   " round(r(sd),.001)

di ""
}
*
}
*



* Panel 2
{
use "$data/nrega_gujarat.dta", clear
distinct Panchayat
di r(N)

gen TotalpersondaysworkedSCST = (TotalpersondaysworkedbySCs + TotalpersondaysworkedbySTs)/100
replace TotalpersondaysworkednonSC = TotalpersondaysworkednonSC/100

gen TotalhouseholdsworkedSCST = TotalSChouseholdsworked  + TotalSThouseholdsworked

foreach out in TotalpersondaysworkedSCST TotalpersondaysworkednonSC TotalhouseholdsworkedSCST TotalhouseholdsworkednonSC {
qui sum `out'
di "`out' " round(r(mean),.001) "   " round(r(sd),.001)

di ""
}
*
}
*



* Panel 3
{
use "$data/budgets.dta", clear
distinct budgetid
di r(N)

foreach out in expenditure grantexp ownfundexp miscexp income grantinc ownfundinc miscinc {
replace `out' = `out'/1000

qui sum `out'
di "`out' " round(r(mean),.001) "   " round(r(sd),.001)

di ""
}
*
}
*

}
*



* Table A5
{
* Dataset
{
import excel "$data/Census_2011.xlsx", sheet("Village_Data_2400") firstrow clear

tostring VillageCode, replace
ren VillageCode VillageCensusCode2011
keep TotalPopulation VillageCensusCode2011 TotalGeographicalAreainHect TotalHouseholds TotalMalePopulationofVillage TotalFemalePopulationofVilla TotalScheduledCastesMalePop TotalScheduledCastesFemaleP TotalScheduledTribesMalePop TotalScheduledTribesFemaleP GovtSecondarySchoolNumbers PrivateSecondarySchoolNumbe GovtSeniorSecondarySchoolNu BN CommunityHealthCentreNumbers CommunityHealthCentreDoctors CommunityHealthCentreParaMed PrimaryHealthCentreNumbers PrimaryHealthCentreDoctorsT PrimaryHealthCentreParaMedi PrimaryHeallthSubCentreNumb PrimaryHeallthSubCentreDocto PrimaryHeallthSubCentrePara MaternityAndChildWelfareCent EV EX TBClinicNumbers TBClinicDoctorsTotalStrength TBClinicParaMedicalParaMedi HospitalAllopathicNumbers HospitalAllopathicDoctorsTota HospitalAllopathicParaMedical MobileHealthClinicNumbers MobileHealthClinicDoctorsTot MobileHealthClinicParaMedica FamilyWelfareCentreNumbers FamilyWelfareCentreDoctorsTo FamilyWelfareCentreParaMedic TapWaterTreatedStatusA1N TapWaterTreatedFunctioningAl TapWaterUntreatedStatusA1 TapWaterUntreatedFunctioning CoveredWellStatusA1NA2 CoveredWellFunctioningAllrou UncoveredWellStatusA1NA UncoveredWellFunctioningAll HandPumpStatusA1NA2 HandPumpFunctioningAllround TubeWellsBoreholeStatusA1 TubeWellsBoreholeFunctioning SpringStatusA1NA2 SpringFunctioningAllroundthe RiverCanalStatusA1NA2 RiverCanalFunctioningAllroun TankPondLakeStatusA1NA2 TankPondLakeFunctioningAllr ClosedDrainageStatusA1NA OpenDrainageStatusA1NA2 CommunityToiletComplexinclud CommunityToiletComplexexclud RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof
ren TotalPopulation T_P_2011

merge 1:m VillageCensusCode2011 using "$data/vill_lgd_census11_01.dta", gen(merge)

* Drop 2011 only villages (since 2001 GP population is unknown)
drop if merge==1
drop merge
}
save "$data/baseline.dta", replace



* Demographics, Area
{
use "$data/baseline.dta", clear
collapse (sum) TotalGeographicalAreainHect TotalHouseholds TotalMalePopulationofVillage TotalFemalePopulationofVilla TotalScheduledCastesMalePop TotalScheduledCastesFemaleP TotalScheduledTribesMalePop TotalScheduledTribesFemaleP (mean) gp_pop, by(gp_id)

drop if gp_id==.

gen prop_female = TotalFemalePopulationofVilla/(TotalMalePopulationofVillage+TotalFemalePopulationofVilla)
gen prop_sc = (TotalScheduledCastesMalePop+TotalScheduledCastesFemaleP)/(TotalMalePopulationofVillage+TotalFemalePopulationofVilla)
gen prop_st = (TotalScheduledTribesMalePop+TotalScheduledTribesFemaleP)/(TotalMalePopulationofVillage+TotalFemalePopulationofVilla)

set more off
foreach x in prop_female prop_sc prop_st TotalGeographicalAreainHect {
di "`x'"

foreach c in 5001 4001 6001 8001 {

qui rdrobust `x' gp_pop if inrange(gp_pop,`c'-1000,`c'+999), c(`c') all bwselect(msetwo) vce(cluster gp_pop)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
di "  "
}
*

}
*



* Public Goods
{
use "$data/baseline.dta", clear
gen secondary_srsecondary 	= GovtSecondarySchoolNumbers + PrivateSecondarySchoolNumbe + GovtSeniorSecondarySchoolNu + BN 
gen healthcenters_clinics = CommunityHealthCentreNumbers + PrimaryHealthCentreNumbers + PrimaryHeallthSubCentreNumb + MobileHealthClinicNumbers + TBClinicNumbers
gen hospitals = HospitalAllopathicNumbers 
gen welfarecenters = MaternityAndChildWelfareCent + FamilyWelfareCentreNumbers


* Fix A(1)/NA(2) coding
{
foreach x in TapWaterTreatedStatusA1N TapWaterTreatedFunctioningAl TapWaterUntreatedStatusA1 TapWaterUntreatedFunctioning CoveredWellStatusA1NA2 CoveredWellFunctioningAllrou UncoveredWellStatusA1NA UncoveredWellFunctioningAll HandPumpStatusA1NA2 HandPumpFunctioningAllround TubeWellsBoreholeStatusA1 TubeWellsBoreholeFunctioning SpringStatusA1NA2 SpringFunctioningAllroundthe RiverCanalStatusA1NA2 RiverCanalFunctioningAllroun TankPondLakeStatusA1NA2 TankPondLakeFunctioningAllr ClosedDrainageStatusA1NA OpenDrainageStatusA1NA2 CommunityToiletComplexinclud CommunityToiletComplexexclud RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof {
replace `x' = . if `x'==0
replace `x' = `x'-2
}
* Now, x=-1 means the public good is present, x = 0 if not; need this for the collapse command

collapse (sum) secondary_srsecondary healthcenters hospitals welfarecenters (min)  TapWaterTreatedStatusA1N TapWaterTreatedFunctioningAl TapWaterUntreatedStatusA1 TapWaterUntreatedFunctioning CoveredWellStatusA1NA2 CoveredWellFunctioningAllrou UncoveredWellStatusA1NA UncoveredWellFunctioningAll HandPumpStatusA1NA2 HandPumpFunctioningAllround TubeWellsBoreholeStatusA1 TubeWellsBoreholeFunctioning SpringStatusA1NA2 SpringFunctioningAllroundthe RiverCanalStatusA1NA2 RiverCanalFunctioningAllroun TankPondLakeStatusA1NA2 TankPondLakeFunctioningAllr ClosedDrainageStatusA1NA OpenDrainageStatusA1NA2 CommunityToiletComplexinclud CommunityToiletComplexexclud RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof (mean) gp_pop, by(gp_id)

drop if gp_id==.


foreach x in secondary_srsecondary healthcenters hospitals welfarecenters {
replace `x'=`x'>0 if `x'!=.
}


foreach x in TapWaterTreatedStatusA1N TapWaterTreatedFunctioningAl TapWaterUntreatedStatusA1 TapWaterUntreatedFunctioning CoveredWellStatusA1NA2 CoveredWellFunctioningAllrou UncoveredWellStatusA1NA UncoveredWellFunctioningAll HandPumpStatusA1NA2 HandPumpFunctioningAllround TubeWellsBoreholeStatusA1 TubeWellsBoreholeFunctioning SpringStatusA1NA2 SpringFunctioningAllroundthe RiverCanalStatusA1NA2 RiverCanalFunctioningAllroun TankPondLakeStatusA1NA2 TankPondLakeFunctioningAllr ClosedDrainageStatusA1NA OpenDrainageStatusA1NA2 CommunityToiletComplexinclud CommunityToiletComplexexclud RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof {
replace `x' = `x'==-1
}
}


gen tapwater = max(TapWaterTreatedStatusA1N, TapWaterUntreatedStatusA1)
gen drainage = max(ClosedDrainageStatusA1NA, OpenDrainageStatusA1NA2)
gen CommunityToilet = max(CommunityToiletComplexinclud,CommunityToiletComplexexclud)

foreach x in secondary_srsecondary healthcenters hospitals welfarecenters tapwater CoveredWellStatusA1NA UncoveredWellStatusA1NA HandPumpStatusA1NA2 TubeWellsBoreholeStatusA1 SpringStatusA1NA2 RiverCanalStatusA1NA2 TankPondLakeStatusA1NA2 drainage CommunityToilet RuralProductionCentresorSani Communitywastedisposalsystem CommunityBiogasorrecycleof {
di "`x'"

foreach c in 5001 4001 6001 8001 {
qui rdrobust `x' gp_pop if inrange(gp_pop,`c'-1000,`c'+999), c(`c') all bwselect(msetwo) vce(cluster gp_pop)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "
}
di ""
}
*

}
*


}
*



